home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / Internet / HTML Prep < prev    next >
Encoding:
Text File  |  1999-03-04  |  7.1 KB  |  298 lines  |  [TEXT/ToyS]

  1. -- Preferences
  2. property kasVersion : "V1.53"
  3. property kasPrefix : "<!-- (c) 1998 AKUA interactive media AG - GLK OPO %v %d %n -->" -- Set to empty string to prevent prefixing
  4. property kasPrefName : "Unix Textify"
  5. property kasPrefixExt : {".html", ".htm"} -- Only prefix these files
  6.  
  7. property kasWebStarSSI : "<!--#include virtual="
  8. property kasNetScapeSSI : "<!--#include file="
  9. property kasWebStarToNetScapeSSI : true -- Change the above two lines
  10.  
  11. property kasLowerCaseNames : true
  12.  
  13. property kasTypesToDo : {"TEXT"} -- Only run on these types
  14. property kasExtToDo : "" -- Use non-empty match string (e.g. "*.html") to limit to that extension
  15.  
  16. property quote : "•"
  17. property cr : (ASCII character 13)
  18. property lf : (ASCII character 10)
  19. property crlf : cr & lf
  20.  
  21. -- Globals
  22. global gasInfoWind -- Info window
  23. global gasInfoPos -- Position of info window
  24. global gasFoldersToDo -- The folders left to process
  25. global gasConverted -- Number gone!
  26. global gasChecked -- Number checked!
  27. global gasPrefix -- Adjusted kasPrefix (munge the % parms)
  28. global gasIndex -- Incremented # starting at 1
  29.  
  30.  
  31. on open fsObjs
  32.     -- Load prefs, show window
  33.     pfLoad()
  34.     
  35.     -- Set up prefix
  36.     set gasPrefix to munge kasPrefix ¬
  37.         searching for ¬
  38.         "%d" replacing it with (the clock in extended sortable form)
  39.     
  40.     set gasPrefix to munge gasPrefix ¬
  41.         searching for ¬
  42.         "%v" replacing it with kasVersion
  43.     
  44.     set gasPrefix to munge gasPrefix ¬
  45.         searching for ¬
  46.         "%n" replacing it with (gasIndex as string)
  47.     
  48.     set gasConverted to 0
  49.     set gasChecked to 0
  50.     
  51.     set gasInfoWind to display info titled kasPrefName ¬
  52.         located at gasInfoPos ¬
  53.         message "Scanning…"
  54.     
  55.     -- Do files
  56.     set gasFoldersToDo to {}
  57.     
  58.     repeat with fsObj in fsObjs
  59.         set myInfo to (basic info for fsObj)
  60.         
  61.         if (system type of myInfo is "fold") then
  62.             set gasFoldersToDo to gasFoldersToDo & {fsObj}
  63.         else if (system type of myInfo is in kasTypesToDo) then
  64.             if (kasExtToDo is "") or ((collect lines of (catalog name of myInfo) that match kasExtToDo) is not "") then
  65.                 DoOne(fsObj)
  66.             end if
  67.         end if
  68.     end repeat
  69.     
  70.     -- Do folders
  71.     repeat while gasFoldersToDo is not {}
  72.         -- Pop one off the end
  73.         set n to the number of items of gasFoldersToDo
  74.         set fsObj to item n of gasFoldersToDo
  75.         
  76.         if (n > 1) then
  77.             set gasFoldersToDo to items 1 through (n - 1) of gasFoldersToDo
  78.         else
  79.             set gasFoldersToDo to {}
  80.         end if
  81.         
  82.         display info gasInfoWind ¬
  83.             message ("Folders to go: " & n) ¬
  84.             at line 6 ¬
  85.             using color (15 * 32)
  86.         
  87.         -- Process it
  88.         GoDeep(fsObj)
  89.     end repeat
  90.     
  91.     display info gasInfoWind message "DONE!"
  92.     
  93.     pause for 5 with seconds timing -- Let screen wait...
  94.     
  95.     set gasInfoPos to screen location of ¬
  96.         (display info gasInfoWind with disposal)
  97.     
  98.     pfSave() -- Save window location
  99. end open
  100.  
  101.  
  102. on DoOne(fsObj)
  103.     set aFileInfo to (alias info from fsObj)
  104.     
  105.     display info gasInfoWind ¬
  106.         message "File: " & (original name of aFileInfo) ¬
  107.         at line 2
  108.     
  109.     set gasChecked to gasChecked + 1
  110.     
  111.     display info gasInfoWind ¬
  112.         message ("Checked: " & gasChecked) ¬
  113.         at line 7 ¬
  114.         using color 15
  115.     
  116.     ConvertFile(fsObj)
  117. end DoOne
  118.  
  119.  
  120. on ConvertFile(fsObj)
  121.     try
  122.         set fileRef to ¬
  123.             open fork from fsObj ¬
  124.                 with write access
  125.     on error errStr
  126.         display info gasInfoWind ¬
  127.             message ("Error: " & errStr) ¬
  128.             at line 12 ¬
  129.             using color (25 * 1024)
  130.         return
  131.     end try
  132.     
  133.     set fname to catalog name of (basic info for fsObj)
  134.     
  135.     set fileData to read data from fileRef
  136.     set newData to fileData
  137.     
  138.     -- Convert DOS to Unix
  139.     set newData to munge newData ¬
  140.         searching for crlf ¬
  141.         replacing it with lf
  142.     
  143.     -- Convert Mac to Unix
  144.     set newData to munge newData ¬
  145.         searching for cr ¬
  146.         replacing it with lf
  147.     
  148.     -- Convert SSI?
  149.     if (kasWebStarToNetScapeSSI) then ¬
  150.         set newData to munge newData ¬
  151.             searching for kasWebStarSSI ¬
  152.             replacing it with kasNetScapeSSI
  153.     
  154.     -- Rename to lowercase?
  155.     set changed to false
  156.     if (kasLowerCaseNames) then
  157.         set flow to transcribe fname with «class ñLow»
  158.         considering case
  159.             if flow is not fname then
  160.                 collate fsObj renaming it to flow
  161.                 set changed to (kasPrefix is not "") -- Prefix if this is a change
  162.             end if
  163.         end considering
  164.     end if
  165.     
  166.     if changed or (newData is not fileData) then
  167.         -- Check prefix
  168.         if (kasPrefix is not "") then
  169.             -- Does this file get prefixes?
  170.             set doThis to (kasPrefixExt is {})
  171.             
  172.             repeat with suffix in kasPrefixExt
  173.                 display info gasInfoWind ¬
  174.                     message (the text from character -(length of suffix) to -1 of fname) ¬
  175.                     at line 15
  176.                 
  177.                 display info gasInfoWind ¬
  178.                     message suffix ¬
  179.                     at line 18
  180.                 
  181.                 set fext to (the text from character -(length of suffix) to -1 of fname)
  182.                 set os to offset of fext in suffix
  183.                 
  184.                 -- A check of equality fails here - I don't know why the hell?!?
  185.                 -- if (fext is suffix) then
  186.                 if ((offset of fext in suffix) is 1) then set doThis to true
  187.             end repeat
  188.             
  189.             if (doThis) then
  190.                 display info gasInfoWind ¬
  191.                     message ("Prefix?") ¬
  192.                     at line 16
  193.                 
  194.                 -- Lop off current prefix
  195.                 if (the text from character 1 to 8 of kasPrefix) is (the text from character 1 to 8 of newData) then
  196.                     set n to offset of cr in newData
  197.                     if (n is 0) then set n to offset of lf in newData
  198.                     if n is not 0 then
  199.                         set newData to gasPrefix & (the text from character n to -1 of newData)
  200.                         display info gasInfoWind ¬
  201.                             message ("Updated prefix") ¬
  202.                             at line 17
  203.                     end if
  204.                 else
  205.                     set newData to gasPrefix & lf & newData
  206.                     
  207.                     display info gasInfoWind ¬
  208.                         message ("Adding prefix") ¬
  209.                         at line 17
  210.                 end if
  211.             else
  212.                 display info gasInfoWind ¬
  213.                     message ("No Prefix") ¬
  214.                     at line 16
  215.             end if
  216.         end if
  217.         
  218.         -- Reset file to 0 long
  219.         size fork fileRef ¬
  220.             given «class len »:0
  221.         
  222.         -- Write data
  223.         write data to fileRef ¬
  224.             from buffer newData
  225.         
  226.         set gasConverted to gasConverted + 1
  227.         
  228.         display info gasInfoWind ¬
  229.             message ("Converted: " & gasConverted) ¬
  230.             at line 8 ¬
  231.             using color (15 * 1024)
  232.         display info gasInfoWind ¬
  233.             message ("Last: " & fname) ¬
  234.             at line 9 ¬
  235.             using color (16 * 1024)
  236.     end if
  237.     
  238.     close fork fileRef
  239. end ConvertFile
  240.  
  241.  
  242.  
  243. on GoDeep(foldObj)
  244.     display info gasInfoWind ¬
  245.         message "Path: " & (foldObj as string)
  246.     
  247.     set daddy to foldObj as string
  248.     
  249.     -- Do kinds that match
  250.     display info gasInfoWind ¬
  251.         message "Scanning files" at line 5
  252.     
  253.     if (kasExtToDo is "") then
  254.         set myItems to the entries in foldObj ¬
  255.             whose types are in kasTypesToDo
  256.     else
  257.         set myItems to the entries in foldObj ¬
  258.             whose types are in kasTypesToDo ¬
  259.             whose names match kasExtToDo
  260.     end if
  261.     
  262.     repeat with myItem in myItems
  263.         DoOne((daddy & myItem) as alias)
  264.     end repeat
  265.     
  266.     -- Do folders
  267.     display info gasInfoWind ¬
  268.         message "Scanning subfolders" at line 5
  269.     
  270.     set myItems to the entries in foldObj ¬
  271.         whose kinds are a folder
  272.     
  273.     repeat with myItem in myItems
  274.         set gasFoldersToDo to gasFoldersToDo & {(daddy & myItem) as alias}
  275.     end repeat
  276.     
  277.     -- Done
  278.     display info gasInfoWind ¬
  279.         message "…" at line 5
  280. end GoDeep
  281.  
  282.  
  283. on pfLoad()
  284.     try
  285.         set ourPrefs to (load preference named kasPrefName)
  286.         set gasInfoPos to item 1 of ourPrefs
  287.         set gasIndex to item 2 of ourPrefs
  288.     on error
  289.         set gasInfoPos to {-1, -1}
  290.         set gasIndex to 1
  291.     end try
  292. end pfLoad
  293.  
  294.  
  295. on pfSave()
  296.     save preference {gasInfoPos, gasIndex + 1} named kasPrefName
  297. end pfSave
  298.